<?php

namespace App\Http\Services;

use App\Http\Model\FinanceDiamond;
use App\Http\Model\FinanceWealth;
use App\Http\Model\Hint;
use App\Http\Model\User as UserModel;
use App\Http\Model\GiftBag as GiftBagModel;
use App\Http\Model\Gift as GiftModel;
use App\Http\Model\Room as RoomModel;
use App\Http\Model\FinanceRoomWealth as FinanceRoomWealthModel;
use App\Http\Model\FinanceCharm as FinanceCharmModel;
use App\Http\Model\FinanceKk as FinanceKkModel;
use App\Http\Model\FinanceUserWallet as FinanceUserWalletModel;
use App\Lib\Publisher;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use App\Http\Model\GiftLibrary;

class GiftBagServices
{
    protected $userModel;
    protected $giftBagModel;// 礼物背包 model
    protected $giftModel;
    protected $roomModel;
    protected $financeRoomWealthModel;// 房间财富
    protected $ratioConf;// 支付比例
    protected $descConf;// 描述文案
    protected $financeCharmModel;//魅力值
    protected $financeKkModel; //金币
    protected $financeUserWalletModel;
    protected $financeWealthModel;
    protected $financeDiamondModel;

    public function __construct(UserModel $userModel, GiftBagModel $giftBagModel, GiftModel $giftModel, RoomModel $roomModel, FinanceRoomWealthModel $financeRoomWealthModel, FinanceCharmModel $financeCharmModel, FinanceKkModel $financeKkModel, FinanceUserWalletModel $financeUserWalletModel)
    {
        $this->userModel = $userModel;
        $this->giftBagModel = $giftBagModel;
        $this->giftModel = $giftModel;
        $this->roomModel = $roomModel;
        $this->financeRoomWealthModel = $financeRoomWealthModel;
        $this->ratioConf = config('pay.wallet_change_ratio');
        $this->descConf = config('pay.wallet_change_desc');
        $this->financeCharmModel = $financeCharmModel;
        $this->financeKkModel = $financeKkModel;
        $this->financeUserWalletModel = $financeUserWalletModel;
        $this->financeWealthModel = new FinanceWealth();
        $this->financeDiamondModel = new FinanceDiamond();
    }

    /**
     * 礼物背包
     * */
    public function giftBagList($params = [])
    {
        $code = Hint::SUCCESS;
        $data = [];

        do {
            try {
                // 获取当前用户信息
                $currentUser = $this->userModel->get_current_user();
                $userId = $currentUser['user_id'] ?? 0;
                if (empty($userId)) {
                    $code = Hint::LOGIN_OUT;
                    break;
                }

                // 发送规格个数
                $specList = $this->giftModel->getGiftSpec();
                $data['spec_list'] = array_column($specList, 'spec_num');

                $page = $params['page'] ?? 1;// 页数
                $limit = $params['limit'] ?? 10;// 条数
                if (!is_numeric($page) or !is_numeric($limit)) {
                    $code = Hint::PARAM_ERROR;
                    break;
                }

                // 条件
                $testUser = $this->userModel->get_test_users();
                if(in_array($userId,$testUser)){
                    $condition = [['user_id', '=', $userId], ['avail', '>', 0]];
                }else{
                    $condition = [['user_id', '=', $userId], ['avail', '>', 0],['gift_id','!=',226]];
                }
                
                // 总条数
                $totalCount = $this->giftBagModel->giftBagTotal($condition);
                // 当前分页数据
                $result = $this->giftBagModel->giftBagList($condition, $page, $limit);
                $data['curr_page'] = (is_numeric($page) && $page > 0) ? intval($page) : 0;// 当前页
                $data['limit'] = (is_numeric($limit) && $limit > 0) ? intval($limit) : 0;// 每页条数
                $data['total_count'] = (is_numeric($totalCount) && $totalCount) ? intval($totalCount) : 0;// 总条数
                $data['total_page'] = (is_numeric($totalCount) && is_numeric($limit) && $totalCount > 0 & $limit > 0) ? ceil($totalCount / $limit) : 0;// 总页数
                $data['list'] = is_array($result) ? $result : array();// 数据

            } catch (\Exception $e) {
                $code = Hint::ERR_SYS_FAIL;
                $data['error_msg'] = Hint::getErrCodeMsg($code) . ':' . $e->getMessage();
                break;
            }

        } while (false);

        return compact('code', 'data');
    }

    //检测用户是否为账号异常
    public function checkAbnormalUser($to_user_ids)
    {
        $code = Hint::SUCCESS;
        $msg = Hint::getErrCodeMsg($code);
        $data = [];

        $users = $this->userModel->get_users_by_ids($to_user_ids);

        if ($users) {
            $is_abnormal = 1;

            $res = array_values(array_filter($users, function ($param) use ($is_abnormal) {
                return $param['is_abnormal'] == $is_abnormal;
            }));

            if ($res) {
                $code = Hint::USER_ACCOUNT_IS_ABNORMAL;
                $msg = Hint::getErrCodeMsg($code);
                $data['user_ids'] = array_column($res, 'user_id');
                $data['user_nicknames'] = array_column($res, 'nickname');
            }
        }
        return compact('code', 'msg', 'data');
    }

}
